package com.egao.common.manage.college;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.employment.entity.EmploymentProcess;
import com.egao.common.module.employment.service.EmploymentProcessService;
import com.egao.common.module.system.entity.College;
import com.egao.common.module.system.service.CollegeService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * @author lym
 * @date 2023/4/26 11:57
 */
@Api(tags = "学院后期促进就业过程管理")
@RestController
@RequestMapping("${api}")
public class CollegeEmploymentProcessController extends BaseController {

    @Autowired
    private CollegeService collegeService;
    @Autowired
    private EmploymentProcessService employmentProcessService;

    @PreAuthorize("hasAuthority('college')")
    @ApiOperation(value = "获取就业过程列表")
    @GetMapping("/college/employment-process/list")
    public JsonResult list(String date) {
        String collegeCode = getLoginUser().getCollegeCode();
        if (StrUtil.isBlank(collegeCode)) {
            return JsonResult.error("没有查询到您的学院代码");
        }

        College college = collegeService.getByCode(collegeCode);
        if (college == null) {
            return JsonResult.error("没有查询到您的学院信息");
        }

        Date d = new Date();
        if (!StrUtil.isBlank(date)) {
            DateTime parse = DateUtil.parse(date);
            d = parse.toJdkDate();
        }
        List<EmploymentProcess> list = employmentProcessService.listCollegeWeek(college.getId(), d);
        if (list.size() == 0) {
            list = employmentProcessService.listTemplate();
        }

        Date startOfWeek = DateUtil.beginOfWeek(d);
        Date endOfWeek = DateUtil.endOfWeek(d);
        String start = DateUtil.format(startOfWeek, DatePattern.NORM_DATE_PATTERN);
        String end = DateUtil.format(endOfWeek, DatePattern.NORM_DATE_PATTERN);

        for (EmploymentProcess item : list) {
            item.setStartTime(startOfWeek);
            item.setEndTime(endOfWeek);
        }

        return JsonResult.ok().setData(list).put("range", start + "~" + end);
    }

    @PreAuthorize("hasAuthority('college')")
    @OperLog(value = "就业过程管理", desc = "保存", result = true)
    @ApiOperation(value = "保存")
    @PostMapping("/college/employment-process/save")
    public JsonResult save(@RequestBody List<EmploymentProcess> list) {
        String collegeCode = getLoginUser().getCollegeCode();
        College college = collegeService.getByCode(collegeCode);
        for (EmploymentProcess item : list) {
            item.setCollegeId(college.getId());
            item.setUserId(getLoginUserId());
            employmentProcessService.saveOrUpdate(item);
        }
        return JsonResult.ok("保存成功");
    }

}
